<?php

namespace App\Http\Controllers\Admin\Trade;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Custom\Business\Order;
use App\B2BProject\Custom\Member;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    use AdminControllerBuildTrait;
    protected $moduleName = '';

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (\Request::ajax()) {

            $query = Order::orderBy('created_at', 'desc');

            $name = $request->query('filter_val');
            if ($request->query('filter_type') == 1) { //订单号
                $query = $query->where('order_code', $name);

            } elseif ($request->query('filter_type') == 2) { //供应商

                $provider_id = Member::where('user', 'like', "%$name%")->first()->id;
                $query       = $query->where('provider_id', $provider_id);
            } else {
                if ($request->query('filter_type') == 3) { //采购商

                    $member_id = Member::where('user', 'like', "%$name%")->first()->id;
                    $query     = $query->where('member_id', $member_id);
                }
            }

            if ($start = $request->query('start_time')) {  //下单时间
                $query = $query->where('created_at', '>=', date('Y-m-d', strtotime($start)));
            }
            if ($end = $request->query('end_time')) {
                $query = $query->where('created_at', '<=', date('Y-m-d', strtotime($end)));
            }
            if ($pay_type = $request->query('payment')) {
                $query = $query->where('pay_type', $pay_type); //支付方式
            }

            if ($status = $request->query('order_state')) {
                $query = $query->where('status', $status); //订单状态
            }


            //日期时间搜索
            if ($start = $request->query('start_time')) {  //下单时间
                $query = $query->where('created_at', '>=', date('Y-m-d', strtotime($start)));
            }
            if ($end = $request->query('end_time')) {
                $query = $query->where('created_at', '<=', date('Y-m-d', strtotime($end)));
            }

            $orders = $query->paginate($request->query('pageSize', 7));

            $data = [];

            foreach ($orders as $order) {
                $item   = [
                    'id'              => $order->id,
                    'order_code'      => $order->order_code,
                    'provider_name'   => Member::where('id', $order->provider_id)->first()->user, //供应商
                    'purchaser_name'  => Member::where('id', $order->member_id)->first()->user,
                    'pay_time'        => date('Y-m-d G:i:s', strtotime($order->created_at)),
                    'goods_total'     => $order->total_amount,
                    'logistics_price' => $order->logistics_price,
                    'order_total'     => $order->logistics_price + $order->total_amount,
                    'pay_type'        => $order->pay_type,
                    'status'          => $order->status,
                    'detail_url'      => '?detail_url&aa=11' //查看
                ];

                switch ($order->status){
                    case 1 :
                        $item['status'] = '等待物流报价';
                        break;
                    case 2 :
                        $item['status'] = '待付款';
                            break;
                    case 3 :
                        $item['status'] = '待发货';;
                            break;
                    case 4 :
                        $item['status'] = '已发货';
                            break;
                    case 5 :
                        $item['status'] = '已收货';
                        break;
                    default:
                        $item['status'] = '资金结算完毕';
                        break;
                }
                $data[] = $item;
            }
            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $orders->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $orders->total(),
                ],
            ]);
        }
        return view('admin/trade/order/index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
